<?php 
class Document extends Module
{
	function Document($row)
	{
		Module::Module($row);
		
		$cmd=Url::get('cmd');
		
		require_once("forms/DocumentList.php");
		
		switch($cmd)
		{
			case 'delete':
				$this->add_form(new Deletedocument());
				break;
			case 'change':
				$this->add_form(new PublicdocumentForm());
				break;
		    default:
		    	$id = (int)Url::get('id');
		    	
				$azname = trim(URL::get("azname",''));
				
				if($id || $azname != '')
				{
					if($id && $azname!='')
					{
						$document = DB::select("document","id=$id");
						
						if($document && $azname == AZLib::safe_title($document['title']) && $document['file_path'])
						{
							$file_src = DATA_PATH."document/{$document['file_path']}";
							
							if(file_exists(ROOT_PATH.$file_src))
							{
								$ext = AZLib::getExtension($file_src);
								
								if($ext && strlen($ext) <= 5)
								{
									$ext_app = substr($ext,1);
									
									if($document['status'] == 1 || User::haveAnyPermission(ADMIN_DOCUMENT))
									{
										DB::query("UPDATE document SET down_num=down_num+1, time_last=".TIME_NOW." WHERE id=$id");
										DB::close();
										
										header("Content-type: application/$ext_app");
										header("Content-Disposition: attachment; filename=".date("Y.m.d-H\hi",$document['time_m'])."-$id-$azname$ext");
										
										readfile(ROOT_PATH.$file_src);
										exit;
									}
								}
							}
						}
					}
					
					header("HTTP/1.0 404 Not Found");
					echo "<h1>404 - Not Found!<br />Return to <a href='".WEB_ROOT."'>".WEB_NAME."</a></h1>";
					exit;
				}
				
				$this->add_form(new ListdocumentForm());
				break;
		}
	}
}
?>